package com.kaigejava.demo.business.web;

import com.kaigejava.demo.business.common.GlobalMap;
import com.kaigejava.demo.business.enums.TypeEnum;
import com.kaigejava.demo.business.service.WebSocketService;
import com.kaigejava.demo.business.service.grab.BilibiliLiveGrabService;
import com.kaigejava.demo.business.service.grab.DouYinGrabService;
import com.kaigejava.demo.business.service.grab.DouYuGrabService;
import com.kaigejava.demo.business.service.grab.HuYaGrabService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.Map;

/**
 * @BelongsProject: douyin-live-java
 * @BelongsPackage: com.kaigejava.demo.business.web
 * @Author: kaigejava
 * @CreateTime: 2023-11-13  17:03
 * @Description: TODO
 * @Version: 1.0
 */
@RequestMapping
@Controller
public class CloseController {


    @Resource
    private  BilibiliLiveGrabService bilibiliLiveGrabService;

    @Resource
    private  DouYinGrabService douYinGrabService;

    @Resource
    private  DouYuGrabService douYuGrabService;

    @Resource
    private  HuYaGrabService huYaGrabService;

    @RequestMapping("/close")
    public String onClose( String type,  String roomId) throws Exception {
        System.out.println("强制断开连接");
        if (type.equals(TypeEnum.B_STATION.getCode())) {
            //处理B站获取弹幕的
            bilibiliLiveGrabService.close(roomId);
        }else if (type.equals(TypeEnum.DY_STATION.getCode())) {
            //处理B站获取弹幕的
            douYinGrabService.close(roomId);
        } else if (type.equals(TypeEnum.DYU_STATION.getCode())) {
            //处理斗鱼获取弹幕的
            douYuGrabService.close(roomId);
        } else if (type.equals(TypeEnum.HY_STATION.getCode())) {
            //处理虎牙获取弹幕的
            huYaGrabService.close(roomId);
        }
        String typeAndRoom = type + "_" + roomId;
        //将这个房间中所有的都移除
        for (Map.Entry<String, WebSocketService> stringWebSocketServiceEntry : GlobalMap.webSocketMap.entrySet()) {
            String key = stringWebSocketServiceEntry.getKey();
            if(key.contains(typeAndRoom)){
                System.out.println("开始移除key:"+key);
                GlobalMap.webSocketMap.remove(key);
            }
        }
        System.out.println("【websocket消息】连接断开,总数:" + GlobalMap.webSocketMap.size());
        return "close-plus";
    }
}
